﻿using System;
using UnityEngine;

// Token: 0x02000125 RID: 293
[DisallowMultipleComponent]
[AddComponentMenu("RVP/C#/Camera/Camera Control", 0)]
[RequireComponent(typeof(Camera))]
[RequireComponent(typeof(AudioListener))]
public class CameraControl : MonoBehaviour
{
	// Token: 0x06000566 RID: 1382 RVA: 0x00029773 File Offset: 0x00027973
	private void Start()
	{
		this.tr = base.transform;
		this.cam = base.GetComponent<Camera>();
		this.Initialize();
	}

	// Token: 0x06000567 RID: 1383 RVA: 0x00029794 File Offset: 0x00027994
	public void Initialize()
	{
		if (!this.lookObj)
		{
			GameObject gameObject = new GameObject("Camera Looker");
			this.lookObj = gameObject.transform;
		}
		if (this.target)
		{
			this.vp = this.target.GetComponent<VehicleParent>();
			this.distance += this.vp.cameraDistanceChange;
			this.height += this.vp.cameraHeightChange;
			this.forwardLook = this.target.forward;
			this.upLook = this.target.up;
			this.targetBody = this.target.GetComponent<Rigidbody>();
		}
		base.GetComponent<AudioListener>().velocityUpdateMode = AudioVelocityUpdateMode.Fixed;
	}

	// Token: 0x06000568 RID: 1384 RVA: 0x00029858 File Offset: 0x00027A58
	private void FixedUpdate()
	{
		if (this.target && this.targetBody && this.target.gameObject.activeSelf)
		{
			if (this.vp.groundedWheels > 0)
			{
				this.targetForward = ((!this.stayFlat) ? this.vp.norm.up : new Vector3(this.vp.norm.up.x, 0f, this.vp.norm.up.z));
			}
			this.targetUp = ((!this.stayFlat) ? this.vp.norm.forward : GlobalControl.worldUpDir);
			Vector3 a = this.lookDir;
			Vector3 b;
			if (this.xInput == 0f && this.yInput == 0f)
			{
				b = Vector3.forward;
			}
			else
			{
				Vector3 vector = new Vector3(this.xInput, 0f, this.yInput);
				b = vector.normalized;
			}
			this.lookDir = Vector3.Slerp(a, b, 0.1f * TimeMaster.inverseFixedTimeFactor);
			this.smoothYRot = Mathf.Lerp(this.smoothYRot, this.targetBody.angularVelocity.y, 0.02f * TimeMaster.inverseFixedTimeFactor);
			RaycastHit raycastHit;
			if (Physics.Raycast(this.target.position, -this.targetUp, out raycastHit, 1f, this.castMask) && !this.stayFlat)
			{
				this.upLook = Vector3.Lerp(this.upLook, ((double)Vector3.Dot(raycastHit.normal, this.targetUp) <= 0.5) ? this.targetUp : raycastHit.normal, 0.05f * TimeMaster.inverseFixedTimeFactor);
			}
			else
			{
				this.upLook = Vector3.Lerp(this.upLook, this.targetUp, 0.05f * TimeMaster.inverseFixedTimeFactor);
			}
			this.forwardLook = Vector3.Lerp(this.forwardLook, this.targetForward, 0.05f * TimeMaster.inverseFixedTimeFactor);
			this.lookObj.rotation = Quaternion.LookRotation(this.forwardLook, this.upLook);
			this.lookObj.position = this.target.position;
			Vector3 normalized = (this.lookDir - new Vector3(Mathf.Sin(this.smoothYRot), 0f, Mathf.Cos(this.smoothYRot)) * Mathf.Abs(this.smoothYRot) * 0.2f).normalized;
			Vector3 forward = this.lookObj.TransformDirection(normalized);
			Vector3 vector2 = this.lookObj.TransformPoint(-normalized * this.distance - normalized * Mathf.Min(this.targetBody.velocity.magnitude * 0.05f, 2f) + new Vector3(0f, this.height, 0f));
			if (Physics.Linecast(this.target.position, vector2, out raycastHit, this.castMask))
			{
				this.tr.position = raycastHit.point + (this.target.position - vector2).normalized * (this.cam.nearClipPlane + 0.1f);
			}
			else
			{
				this.tr.position = vector2;
			}
			this.tr.rotation = Quaternion.LookRotation(forward, this.lookObj.up);
		}
	}

	// Token: 0x06000569 RID: 1385 RVA: 0x00029C34 File Offset: 0x00027E34
	public void SetInput(float x, float y)
	{
		this.xInput = x;
		this.yInput = y;
	}

	// Token: 0x0600056A RID: 1386 RVA: 0x00029C44 File Offset: 0x00027E44
	private void OnDestroy()
	{
		if (this.lookObj)
		{
			UnityEngine.Object.Destroy(this.lookObj.gameObject);
		}
	}

	// Token: 0x04000774 RID: 1908
	private Transform tr;

	// Token: 0x04000775 RID: 1909
	private Camera cam;

	// Token: 0x04000776 RID: 1910
	private VehicleParent vp;

	// Token: 0x04000777 RID: 1911
	public Transform target;

	// Token: 0x04000778 RID: 1912
	private Rigidbody targetBody;

	// Token: 0x04000779 RID: 1913
	public float height;

	// Token: 0x0400077A RID: 1914
	public float distance;

	// Token: 0x0400077B RID: 1915
	private float xInput;

	// Token: 0x0400077C RID: 1916
	private float yInput;

	// Token: 0x0400077D RID: 1917
	private Vector3 lookDir;

	// Token: 0x0400077E RID: 1918
	private float smoothYRot;

	// Token: 0x0400077F RID: 1919
	private Transform lookObj;

	// Token: 0x04000780 RID: 1920
	private Vector3 forwardLook;

	// Token: 0x04000781 RID: 1921
	private Vector3 upLook;

	// Token: 0x04000782 RID: 1922
	private Vector3 targetForward;

	// Token: 0x04000783 RID: 1923
	private Vector3 targetUp;

	// Token: 0x04000784 RID: 1924
	[Tooltip("Should the camera stay flat? (Local y-axis always points up)")]
	public bool stayFlat;

	// Token: 0x04000785 RID: 1925
	[Tooltip("Mask for which objects will be checked in between the camera and target vehicle")]
	public LayerMask castMask;
}
